package com.moli.iov.down.command.mq.activemq;

import com.moli.iov.down.model.dto.remoting.CommandDto;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.Session;
import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.ScheduledMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * <p>
 *  ActiveMQ 生产者
 * </p>
 *
 * @author: zql
 * @Date: 2018/11/28 15:23
 */
@Slf4j
@Component("activeMQProducer")
public class ActiveMqProducer {

    @Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    private Queue queue;


    /**
     * 推送
     * @param commandDto
     * @param generalDelayTime  延时时间
     */
    @Async
    public void send(CommandDto commandDto, long generalDelayTime){

        jmsTemplate.send(queue, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {

                log.info("ActiveMQ 发送延时消息开始，入参：{} 延时时间：{}", commandDto, generalDelayTime);

                ObjectMessage objectMessage = session.createObjectMessage(commandDto);
                objectMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, generalDelayTime);
                return objectMessage;
            }
        });
        log.info("ActiveMQ 发送延时消息完成，入参：{}", commandDto);
    }
}
